﻿/*{
author=pk2.2（기마책사、江东新风、白马叔叔、铃）
date=2023/05/17
}*/

// ## 2023/11/17 # 铃 # 完全汉化,优化逻辑 ##
// ## 2023/05/17 # 铃 # 修复大城市人才府会对不同势力小城市加成效果 ##
// ## 2023/05/08 # 铃 # 批量登用功能添加到小城市--注意非己方城市的人才府也会影响小城市行动力 ##
// ## 2021/01/28 # 白马叔叔 # 政治从高到低排序 ##
// ## 2021/01/02 # 江东新风 # 在批量登用时临时将舌战设为不观看，所以平时不需要将舌战设为不观看也可以批量登用 ##
// ## 2020/07/26 ##
/*
@ 만든이: 기마책사
@ UPDATE: '19.5.3    / 최초적용 : 인재探索 连续실행 메뉴
@ UPDATE: '19.5.8    / 변경내용 : 설전 옵션 체크 기능 추가 (설전OFF 시에만 批量登用 사용가능)
*/

namespace 批量登用
{
    //---------------------------------------------------------------------------------------


    class Main
    {
        pk::building@ building;
        pk::force@ force;
        pk::city@ city;
        pk::person@ taishu;
        pk::district@ district;
        pk::list<pk::person@> person_list;
        int ACTION_COST_登用;

        Main()
        {
            pk::menu_item 批量登用;
            批量登用.menu = 102;
            批量登用.pos = 9;
            //批量登用.shortcut = global_menu::shortcut_批量登用;
            批量登用.init = pk::building_menu_item_init_t(init);
            批量登用.is_visible = pk::menu_item_is_visible_t(isVisible_批量登用);
            批量登用.is_enabled = pk::menu_item_is_enabled_t(isEnabled_批量登用);
            批量登用.get_text = pk::menu_item_get_text_t(getText_批量登用);
            批量登用.get_desc = pk::menu_item_get_desc_t(getDesc_批量登用);
            批量登用.handler = pk::menu_item_handler_t(handler_批量登用);
            pk::add_menu_item(批量登用);

        }

        void init(pk::building@ building_t)
        {
            @building = @building_t;
            @force = pk::get_force(building.get_force_id());
            int city_id = pk::get_city_id(building.pos);
            @city = pk::get_city(city_id);
            @taishu = pk::get_person(pk::get_taishu_id(building));
            @district = pk::get_district(building.get_district_id());

            person_list = pk::get_idle_person_list(building);

            ACTION_COST_登用 = get_hrcmd_ap(city, building);

        }


        int get_hrcmd_ap(pk::city@ city_t, pk::building@ building_t)
        {
            int hrcmd_ap = 20;
            if (pk::is_alive(city_t))
            {
                if (pk::has_facility(city, 设施_人材府) and city_t.get_force_id() == building_t.get_force_id())
                    return hrcmd_ap / 2;
            }
            return hrcmd_ap;
        }


        //---------------------------------------------------------------------------------------

        string getText_批量登用()
        {
            return pk::encode("批量登用");
        }

        string getDesc_批量登用()
        {

            if (person_list.count == 0)
                return pk::encode("没有可执行的武将.");
            else if (int(district.ap) < ACTION_COST_登用)
                return pk::encode(pk::format("行动力不足 (必须 {} 行动力)", ACTION_COST_登用));
            else
                return pk::encode(pk::format("批量登用城市内俘虏 (行动力{})", ACTION_COST_登用));
        }

        bool isEnabled_批量登用()
        {
            //if (debate_opt < 2) return false;

            if (person_list.count == 0) return false;
            else if (int(district.ap) < ACTION_COST_登用) return false;
            return true;
        }

        bool isVisible_批量登用()
        {
            if (pk::is_campaign()) return false;
            if (!pk::is_alive(building)) return false;
            return true;
        }

        bool handler_批量登用()
        {
            if (person_list.count == 0) return false;

            auto prisoner_list = get_prisoner_list(building);
            if (prisoner_list.count == 0) return false;

            int max = pk::min(district.ap / ACTION_COST_登用, prisoner_list.count, person_list.count);
            if (max == 0) return false;

            // 실행무장 선택하기
            pk::list<pk::person@> actors;
            string person_name;
            bool confirm = false;
            while (!confirm)
            {
                actors = pk::person_selector(pk::encode("选择登用执行武将"), pk::encode("请选择要执行登用的武将"), person_list, 1, max, actors, 26);
                int count = int(actors.count);
                if (count == 0) return false;

                person_name = pk::decode(pk::get_name(pk::get_person(actors[0].get_id())));
                if (count > 1)
                    person_name = pk::format("{}及其他{}人", person_name, count - 1);

                confirm = pk::yes_no(pk::encode(pk::format("让\x1b[1x{}\x1b[0x来登用城市内的俘虏吗?", person_name)));
            }
            //暂存舌战配置信息，将舌战设置为不观看，舌战后恢复
            int debate_opt = int(pk::option["Debate"]);
            if (int(pk::option["Debate"]) < 2)
            {
                pk::option["Debate"] = 2;
            }

            //俘虏按综合能力强弱排序
            prisoner_list.sort(function(a, b) {
                int a_statall = 0;
                int b_statall = 0;
                for (int i = 0; i < 武将能力_末; ++i)
                {
                    a_statall += a.stat[i];
                    b_statall += b.stat[i];
                }
                return a_statall > b_statall;
            });

            //按照魅力从低到高排序，然后有特技的排最后
            actors.sort(function(a, b) {
                bool a_skill = ch::has_skill(a, 特技_圣人);
                bool b_skill = ch::has_skill(b, 特技_圣人);
              
                return (a_skill ? (500 + a.stat[武将能力_魅力]) : a.stat[武将能力_魅力]) < (b_skill ? (500 + b.stat[武将能力_魅力]) : a.stat[武将能力_魅力]);
            });

            // 连续 登用

           
            for (int i = 0; i < actors.count; i++)
            {
                pk::person@ actor = @actors[i];

                for (int j = 0; j < prisoner_list.count; j++)
                {
                    auto target = prisoner_list[i];
                    //所以就是用最低魅力的从能力和高到低依次尝试登用俘虏，能成功就登用
                    int seed = pk::create_scenario_time_seed();//产生和登用一样的时间种子
                    if (EMPLOY_CHANCE::get_chance(target, actor, 0, seed))
                    {
                        // 登用
                        pk::employ_cmd_info cmd_employ;
                        @cmd_employ.base = building;
                        @cmd_employ.actor = actor;
                        @cmd_employ.target = target;
                        pk::command(cmd_employ);
                        prisoner_list.remove_at(j);//能登用，就移除对应项目
                        break;//
                    }
                }
              
            }



            //如果在前一个配置时强制退出，则此处大概无法恢复
            pk::option["Debate"] = debate_opt;


            return true;
        }

        pk::list<pk::person@> get_prisoner_list(pk::building@ building)
        {
            pk::list<pk::person@>list;
            int base_id = building.get_id();
            for (int i = 0; i < 非贼武将_末; ++i)
            {
                auto person = pk::get_person(i);
                if (!pk::is_alive(person)) continue;
                if (person.service != base_id) continue;
                if (person.mibun != 身份_俘虏) continue;
                if (person.get_force_id() != -1) continue;
                list.add(person);
            }
            return list;
        }
        //---------------------------------------------------------------------------------------

        bool 调试模式 = false;

    }

    Main main;
}